//
//  SeniorityCollectionViewCell.m
//  NewMusic
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SeniorityCollectionViewCell.h"

@interface SeniorityCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *array;


@end

@implementation SeniorityCollectionViewCell

- (void)dealloc
{
    [_array release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.array = [NSMutableArray array];
        [self customer];
        [self buildTableView];
    }
    return self;
}

- (void)customer
{
    NSString *string = @"http://api.songlist.ttpod.com/channels/bhb/children?app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=c61e1dfaea64217f93ecee8f89d3167d08db491c&idfa=7E083EC9-D67D-4492-8AD9-7AEE22796941&utdid=VhH%2FqR36qsADAM70Q%2FnVNw0E&alf=700606&bundle_id=com.ttpod.music&latitude=38.88254&longtitude=121.5397";
    [CustomerNetworking customerConnectWithURL:string andParameters:nil andBlock:^(id response) {
        NSArray *array = response;
        for (NSDictionary *dictionary in array) {
            SeniorityModel *seniorityModel = [SeniorityModel seniorityModelWithDictionary:dictionary];
            [_array addObject:seniorityModel];
            
        }
        [self.tableView reloadData];
    }];
}

- (void)buildTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height + 47) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[SeniorityTableViewCell class] forCellReuseIdentifier:@"seniorityTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
    //分组头视图
    
    
    //tableView尾视图
    UIView *footView = [[UIView alloc] init];
    footView.frame = CGRectZero;
    self.tableView.tableFooterView = footView;
    
    
    [self.contentView addSubview:_tableView];
    [_tableView release];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, 375, 50);
    UILabel *headerLabel = [[[UILabel alloc] init] autorelease];
    headerLabel.font = [UIFont systemFontOfSize:20];
    headerLabel.frame = CGRectMake(10, 0, 375, 30);
    headerLabel.textColor = [UIColor whiteColor];
    [headerView addSubview:headerLabel];
    headerLabel.font = [UIFont systemFontOfSize:17];
    headerLabel.text = [[self.array objectAtIndex:section] name];
    return [headerView autorelease];
}

//返回头视图高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 30;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.array objectAtIndex:section] refs].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SeniorityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"seniorityTableViewCell" forIndexPath:indexPath];
    cell.seniorityDataModel = [[[self.array objectAtIndex:indexPath.section] refs] objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate pushHot:[[[[self.array objectAtIndex:indexPath.section] refs] objectAtIndex:indexPath.row] ranklist_id]];
}

@end
